﻿//// See https://aka.ms/new-console-template for more information
//Console.WriteLine("Hello, World!");
//编写面向对象程序实现长方形、正方形、圆形等形状的类
//1.每个形状类都能计算面积、判断形状是否合法。请尝试合理使用接口/抽象类、属性来实现
//2.随机创建10个形状对象，计算这些对象的面积之和。
using System;
namespace SecondAss;
public abstract class Shape//抽象类
{
    public abstract double CalculateArea();//计算面积额
    public abstract bool isValid();//检查是否合法
}
public class Rectangle:Shape//长方形
{
    public double length;
    public double width;
    public Rectangle(double length, double width)
    {
        this.length = length;
        this.width = width;
    }

    public override double CalculateArea()
        {
            return length * width;
        }

        public override bool isValid()
        {
            if (length < 0 || width < 0) return false;
            else return true;
            //return length > 0 && width > 0;
        }
    }

public class Square : Shape//正方形
{
        public double edge;
    public Square(double edge)
    {
        this.edge = edge;
    }

    public override double CalculateArea()
        {
            return edge * edge;
        }

        public override bool isValid()
        {
            return edge > 0;
        }
    }

public class Circle : Shape//圆形
{
        public double radius;
    public Circle(double radius)
    {
        this.radius = radius;
    }

    public override double CalculateArea()
        {
            return Math.PI * radius * radius;
        }

        public override bool isValid()
        {
            return radius > 0;
        }
}

class Program
{
    static void Main()
    {
        //Rectangle rectangle = new Rectangle(5,10);
        //Console.WriteLine("有一个长方形：" + "长：" + rectangle.length + "，宽：" + rectangle.width);
        //if (rectangle.isValid())
        //{
        //    Console.WriteLine("这个长方形的面积是" + rectangle.CalculateArea()+"。");
        //}
        //else Console.WriteLine("这不是一个长方形。");
        Random seed = new Random();//随机数
        List<Shape> shapes = new List<Shape>();//把随机创建的10个形状放到列表里面存着
        for(int i = 0; i < 10; i++)//随机生成10个形状
        {
            int ShapeType = seed.Next(3);//0,1,2三个形状分别表示长方形，正方形，圆形
            Shape shape = null;
            switch (ShapeType)
            {
                case 0://长方形
                    double length = seed.Next(1, 10);
                    double width = seed.Next(1, 10);
                    shape = new Rectangle(length, width);
                    break;
                case 1://正方形
                    double edge = seed.Next(1, 10);
                    shape = new Square(edge);
                    break;
                case 2://圆形
                    double radius = seed.Next(1, 10);
                    shape = new Circle(radius);
                    break;
            }
            shapes.Add(shape);
        }
        //输出面接
        for(int i = 0; i < 10; i++)
        {
            if (shapes[i].isValid())
            {
                Console.WriteLine("形状类型：" + shapes[i].GetType().Name + "，面积:" + shapes[i].CalculateArea());
            }
            else Console.WriteLine("非法形状！");
        }
        //输出面积之和
        double sum = 0;
        for(int i = 0; i < 10; i++)
        {
            if (shapes[i].isValid())
            {
                sum = sum + shapes[i].CalculateArea();
            }
        }
        Console.WriteLine("面积之和是：" + sum);
    }
}